package com.zking.myservlet.user;

import com.zking.dao.UserDao;
import com.zking.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/manage/userSelectById")
public class UserSelectById extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 处理请求的编码，解决中文乱码
        request.setCharacterEncoding("utf-8");
        // 处理响应的编码，解决中文乱码
        response.setContentType("text/html;charset=utf-8");

        System.out.println("UserSelectById===进来");

        // 从请求参数中获取用户 id 和 pageIndex
        String id = request.getParameter("id");//转化为int类型，但是id是字符串
        int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));

        // 调用 UserDao 获取指定 id 的用户信息
        User user = UserDao.getUserById(id);

        // 设置请求属性，传递到 user-modify.jsp 页面
        request.setAttribute("user", user);
        request.setAttribute("pageIndex", pageIndex);

        // 将请求转发到 user-modify.jsp 页面
        request.getRequestDispatcher("user-modify.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 处理 GET 请求，直接调用 doPost 方法处理
        doPost(request, response);
    }
}
